﻿using IRepositoryBll;
using IRepositoryDal;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryBll
{
    /// <summary>
    /// 部门业务访问层
    /// </summary>
    public class DepartmentInfoBll : IDepartmentInfoBll
    {
        private IDepartmentInfoDal _departmentInfoDal;
        private IUserInfoDal _userInfoDal;

        public DepartmentInfoBll(IDepartmentInfoDal departmentInfoDal, IUserInfoDal userInfoDal)
        {
            _departmentInfoDal = departmentInfoDal;
            _userInfoDal = userInfoDal;
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="departmentInfo"></param>
        /// <returns></returns>
        public bool AddDepartmentInfo(DepartmentInfo departmentInfo)
        {
            //DepartmentInfoDal departmentInfoDal = new DepartmentInfoDal();
            return _departmentInfoDal.Add(departmentInfo);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="departmentInfoId"></param>
        /// <returns></returns>
        public bool DeleteDepartmentInfo(string departmentInfoId)
        {
            return _departmentInfoDal.Delete(departmentInfoId);
        }

        /// <summary>
        /// 根据id获取部门
        /// </summary>
        /// <param name="departmentInfoId"></param>
        /// <returns></returns>
        public DepartmentInfo GetDepartmentInfo(string departmentInfoId)
        {
            return _departmentInfoDal.GetEntity(departmentInfoId);
        }

        public List<DepartmentInfo> GetDepartmentInfoList()
        {
            return _departmentInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false).ToList();
        }
        /// <summary>
        /// 分页、获取部门数据信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="departmentName"></param>
        /// <returns></returns>
        public object GetDepartmentInfoListByPage(int page, int limit, out int count, string departmentName)
        {
            //查询部门信息
            var departmentInfo = _departmentInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false);
            if (!string.IsNullOrEmpty(departmentName))
            {
                departmentInfo = departmentInfo.Where(u => u.DepartmentName.Contains(departmentName));
            }

            //查询出来数据的数量
            count = departmentInfo.Count();
            //获取用户数据集
            var userInfos = _userInfoDal.GetEntitiesDb();

            //查询部门信息
            var allDepartmentInfos = _departmentInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false);

            var tempList = from d in departmentInfo
                           join u in userInfos
                           on d.LeaderID equals u.ID into duTemp
                           from du in duTemp.DefaultIfEmpty()

                           join pd in allDepartmentInfos
                           on d.ParentID equals pd.ID into dpdTemp
                           from dpd in dpdTemp.DefaultIfEmpty()
                           select new
                           {
                               d.ID,
                               d.DepartmentName,
                               d.Description,
                               d.CreateTime,
                               du.UserName,
                               ParentDeparmentName = dpd.DepartmentName
                           };
            //分页
            var res = tempList.OrderBy(d => d.CreateTime).Skip((page - 1) * limit).Take(limit).ToList().Select(u =>
            {
                return new
                {
                    u.ID,
                    u.DepartmentName,
                    u.Description,
                    CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    u.UserName,
                    u.ParentDeparmentName
                };
            });

            return res;
        }

        /// <summary>
        /// 获取部门下拉集合
        /// </summary>
        /// <returns></returns>
        public object GetDepartmentSelectOption()
        {
            object departmentInfos = _departmentInfoDal.GetEntitiesDb().Where(d => !d.IsDelete).Select(d => new
            {
                d.ID,
                d.DepartmentName
            }).ToList();
            return departmentInfos;
        }
        /// <summary>
        /// 部门下拉选数据
        /// </summary>
        /// <param name="departmentInfoId"></param>
        /// <returns></returns>

        public object GetDepartmentSelectOption(string departmentInfoId)
        {
            var departmentInfos = _departmentInfoDal.GetEntitiesDb().Where(d => d.ID != departmentInfoId && !d.IsDelete).Select(d => new
            {
                d.ID,
                d.DepartmentName
            }).ToList();
            return departmentInfos;
        }





        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="departmentInfoId"></param>
        /// <returns></returns>
        public bool SoftDepartmentInfo(string departmentInfoId)
        {
            DepartmentInfo departmentInfo = _departmentInfoDal.GetEntity(departmentInfoId);
            if (departmentInfo != null)
            {
                departmentInfo.IsDelete = true;
                departmentInfo.DeleteTime = DateTime.Now;
                return _departmentInfoDal.Update(departmentInfo);
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="departmentInfo"></param>
        /// <returns></returns>
        public bool UpdateDepartmentInfo(DepartmentInfo departmentInfo)
        {
            return _departmentInfoDal.Update(departmentInfo);
        }
        /// <summary>
        /// 更新部门信息
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="DepartmentName"></param>
        /// <param name="Description"></param>
        /// <param name="LeaderID"></param>
        /// <returns></returns>
        public bool UpdateDepartmentInfo(string departmentInfoId, string departmentName, string description, string leaderID,string parentID)
        {
            DepartmentInfo departmentInfo = _departmentInfoDal.GetEntity(departmentInfoId);
            if (departmentInfo != null)
            {
                departmentInfo.DepartmentName = departmentName;
                departmentInfo.Description = description;
                departmentInfo.LeaderID = leaderID;
                departmentInfo.ParentID = parentID;

                return _departmentInfoDal.Update(departmentInfo);
            }
            else
            {
                return false;
            }
        }
    }
}
